package com.xique.park.bean.transform.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.park.bean.domain.ParkConfig;
import com.xique.park.bean.domain.ParkPayPp;
import com.xique.park.bean.request.web.parkInfo.ParkBaseInfoSaveRequest;
import com.xique.park.bean.request.web.parkInfo.ParkParamConfigSaveRequest;
import com.xique.park.bean.vo.web.parkinfo.ParkBaseInfoVo;
import com.xique.park.bean.vo.web.parkinfo.ParkParamConfigVo;
import com.xique.park.bean.vo.web.parkinfo.ParkPayConfigVo;
import com.xique.system.api.dto.SystemProjectDTO;
import lombok.experimental.UtilityClass;

import java.util.Date;

/**
 * 车场参数数据转化
 *
 * @author xique
 */
@UtilityClass
public class ParkDetailInfoTransform {

    /**
     * 车场基本信息转化
     */
    public static ParkBaseInfoVo transformParkBaseInfoVo(SystemProjectDTO systemProjectDTO, ParkConfig parkConfig) {
        ParkBaseInfoVo parkBaseInfoVo = new ParkBaseInfoVo();
        if (null != systemProjectDTO) {
            parkBaseInfoVo.setProvince(systemProjectDTO.getProvince());
            parkBaseInfoVo.setCity(systemProjectDTO.getCity());
            parkBaseInfoVo.setDistrict(systemProjectDTO.getDistrict());
            parkBaseInfoVo.setAddress(systemProjectDTO.getAddress());
            parkBaseInfoVo.setParkId(systemProjectDTO.getId());
            parkBaseInfoVo.setIsOpenPark(systemProjectDTO.getIsOpenPark());
            parkBaseInfoVo.setFeeRule(systemProjectDTO.getFeeRule());
            if (null == parkConfig) {
                parkBaseInfoVo.setParkPrincipal(systemProjectDTO.getPrincipal());
                parkBaseInfoVo.setParkPrincipalPhone(systemProjectDTO.getPrincipalPhone());
                parkBaseInfoVo.setParkServicePhone(systemProjectDTO.getServicePhone());
                parkBaseInfoVo.setParkVersion(systemProjectDTO.getParkVersion());
                parkBaseInfoVo.setParkStatus(systemProjectDTO.getStatus());
            }
        }
        if (null != parkConfig) {
            parkBaseInfoVo.setId(parkConfig.getId());
            parkBaseInfoVo.setParkVersion(parkConfig.getParkVersion());
            parkBaseInfoVo.setParkStatus(parkConfig.getStatus());
            parkBaseInfoVo.setParkName(parkConfig.getName());
            parkBaseInfoVo.setParkNature(parkConfig.getParkNature());
            parkBaseInfoVo.setParkPrincipal(parkConfig.getPrincipal());
            parkBaseInfoVo.setParkPrincipalPhone(parkConfig.getPrincipalPhone());
            parkBaseInfoVo.setParkServicePhone(parkConfig.getServicePhone());
        }
        return parkBaseInfoVo;
    }

    /**
     * 车场全局参数转化
     */
    public static ParkParamConfigVo transformParkParamConfigVo(ParkConfig parkConfig) {
        ParkParamConfigVo parkParamConfigVo = new ParkParamConfigVo();
        if (null != parkConfig) {
            parkParamConfigVo.setId(parkConfig.getId());
            parkParamConfigVo.setAfterSale(parkConfig.getAfterSale());
            String config = parkConfig.getConfig();
            if (StringUtils.isNotEmpty(config)) {
                JSONObject configObj = JSON.parseObject(config);
                parkParamConfigVo.setEnRemain(configObj.getString("enRemain"));
                parkParamConfigVo.setEnGarageSpaces(configObj.getString("enGarageSpaces"));
                parkParamConfigVo.setPayStayTime(configObj.getInteger("payStayTime"));
                parkParamConfigVo.setPicSaveDays(configObj.getInteger("picSaveDays"));
                parkParamConfigVo.setDataSaveDays(configObj.getInteger("dataSaveDays"));
                parkParamConfigVo.setTrigTime(configObj.getInteger("trigTime"));
                parkParamConfigVo.setMonthBeforeDays(configObj.getInteger("monthBeforeDays"));
                parkParamConfigVo.setMonthAfterDays(configObj.getInteger("monthAfterDays"));
                parkParamConfigVo.setIsDiscount(configObj.getString("isDiscount"));
                parkParamConfigVo.setIsPoliceFee(configObj.getString("isPoliceFee"));
                parkParamConfigVo.setIsVisitor(configObj.getString("isVisitor"));
                parkParamConfigVo.setIsAllowEditInTime(configObj.getString("isAllowEditInTime"));
                parkParamConfigVo.setSaveStatus(Boolean.TRUE);
            } else {
                parkParamConfigVo.setSaveStatus(Boolean.FALSE);
            }
        }
        return parkParamConfigVo;
    }

    /**
     * 基本信息保存转化为车场配置实体
     */
    public static ParkConfig transformBaseInfoSaveRequest(ParkBaseInfoSaveRequest request) {
        ParkConfig parkConfig = new ParkConfig();
        parkConfig.setId(request.getId());
        parkConfig.setParkId(SecurityUtils.getParkId());
        parkConfig.setParkNature(request.getParkNature());
        parkConfig.setName(request.getParkName());
        parkConfig.setUpdateTime(new Date());
        parkConfig.setParkVersion(request.getParkVersion());
        parkConfig.setPrincipal(request.getParkPrincipal());
        parkConfig.setPrincipalPhone(request.getParkPrincipalPhone());
        parkConfig.setServicePhone(request.getParkServicePhone());
        parkConfig.setStatus(request.getParkStatus());
        return parkConfig;
    }

    public static ParkConfig transformParamConfigSaveRequest(ParkParamConfigSaveRequest request) {
        ParkConfig parkConfig = new ParkConfig();
        parkConfig.setId(request.getId());
        parkConfig.setAfterSale(request.getAfterSale());
        parkConfig.setParkId(SecurityUtils.getParkId());
        JSONObject configObj = new JSONObject();
        configObj.put("enRemain", request.getEnRemain());
        configObj.put("enGarageSpaces", request.getEnGarageSpaces());
        configObj.put("payStayTime", request.getPayStayTime());
        configObj.put("picSaveDays", request.getPicSaveDays());
        configObj.put("dataSaveDays", request.getDataSaveDays());
        configObj.put("trigTime", request.getTrigTime());
        configObj.put("monthBeforeDays", request.getMonthBeforeDays());
        configObj.put("monthAfterDays", request.getMonthAfterDays());
        configObj.put("isDiscount", request.getIsDiscount());
        configObj.put("isPoliceFee", request.getIsPoliceFee());
        configObj.put("isVisitor", request.getIsVisitor());
        configObj.put("isAllowEditInTime", request.getIsAllowEditInTime());
        parkConfig.setConfig(configObj.toJSONString());
        return parkConfig;
    }

    /**
     * PP支付配置转化为前端Vo
     */
    public static ParkPayConfigVo transformParkPayConfigVo(ParkPayPp parkPayPp) {
        ParkPayConfigVo parkPayConfigVo = new ParkPayConfigVo();
        parkPayConfigVo.setId(parkPayPp.getId());
        parkPayConfigVo.setAppSecret(parkPayPp.getAppSecret());
        parkPayConfigVo.setParkUuid(parkPayPp.getParkUuid());
        parkPayConfigVo.setServerPort(parkPayPp.getServerPort());
        parkPayConfigVo.setServerUrl(parkPayPp.getServerUrl());
        parkPayConfigVo.setStatus(parkPayPp.getStatus());
        return parkPayConfigVo;
    }

}
